library(tidyverse) # version 2.0.0

### Begin by setting the working directory to the location of the source file ###

### in our case ###
setwd("~/Dropbox/Populism_Experiment/ForPublication")

# load data
data_raw <- read.csv("raw_data.csv")
#rename columns
colnames(data_raw)[c(1, 13:16, 19:23)] <- c("ID", "pop_bat1", "pop_bat2", "pop_bat3", "issue", "statement", "pre_issue", "pre_cand", "post_issue", "post_cand")
# drop unnecessary columns
data_raw <- data_raw %>% select(-c(QNum1a, Qphoto, page_Q1_timing, page_issue_Q2_Q4_timing, page_p_Q3_timing, page_p_Statement_Q4_timing, page_p_q5_timing))

#1. create populist attitudes measure
data_raw$pop_att <- (data_raw$pop_bat1 + data_raw$pop_bat2 + data_raw$pop_bat3) /3
range(data_raw$pop_att)
hist(data_raw$pop_att) # normal distribution with slight shift to the right

#2. create attitude strength level strong =1, medium = 0, weak = -1,

data_raw <- mutate(data_raw,
                   att_strength = 
                      case_when(issue == 1 ~ 1,
                                issue == 2 ~ 1,
                                issue == 3 ~ 1,
                                issue == 4 ~ 0,
                                issue == 5 ~ 0,
                                issue == 6 ~ 0,
                                issue == 7 ~ -1,
                                issue == 8 ~ -1,
                                issue == 9 ~ -1))

#3. create treatment variable (populist/non-populist = 1/0)
data_raw <- mutate(data_raw,
                   treatment = 
                      case_when(statement == 1 ~ 1, #issue1
                                statement == 2 ~ 0,
                                statement == 3 ~ 1,
                                statement == 4 ~ 0,
                                statement == 5 ~ 1, #2
                                statement == 6 ~ 0,
                                statement == 7 ~ 1,
                                statement == 8 ~ 0,
                                statement == 9 ~ 1, #3
                                statement == 10 ~ 0,
                                statement == 11 ~ 1,
                                statement == 12 ~ 0,
                                statement == 13 ~ 1,#4
                                statement == 14 ~ 0,
                                statement == 15 ~ 1,
                                statement == 16 ~ 0,
                                statement == 17 ~ 1, #5
                                statement == 18 ~ 0,
                                statement == 19 ~ 1,
                                statement == 20 ~ 0,
                                statement == 21 ~ 1, #6
                                statement == 22 ~ 0,
                                statement == 23 ~ 1,
                                statement == 24 ~ 0,
                                statement == 25 ~ 1, #7
                                statement == 26 ~ 0,
                                statement == 27 ~ 1,
                                statement == 28 ~ 0,
                                statement == 29 ~ 1, #8
                                statement == 30 ~ 0,
                                statement == 31 ~ 1,
                                statement == 32 ~ 0,
                                statement == 33 ~ 1, #9
                                statement == 34 ~ 0,
                                statement == 35 ~ 1,
                                statement == 36 ~ 0))
# 4. create direction variable 
data_raw <- mutate(data_raw,
                   direction = 
                     case_when(statement == 1 ~ 1, #issue1
                               statement == 2 ~ 1,
                               statement == 3 ~ -1,
                               statement == 4 ~ -1,
                               statement == 5 ~ 1, #2
                               statement == 6 ~ 1,
                               statement == 7 ~ -1,
                               statement == 8 ~ -1,
                               statement == 9 ~ 1, #3
                               statement == 10 ~ 1,
                               statement == 11 ~ -1,
                               statement == 12 ~ -1,
                               statement == 13 ~ 1,#4
                               statement == 14 ~ 1,
                               statement == 15 ~ -1,
                               statement == 16 ~ -1,
                               statement == 17 ~ 1, #5
                               statement == 18 ~ 1,
                               statement == 19 ~ -1,
                               statement == 20 ~ -1,
                               statement == 21 ~ 1, #6
                               statement == 22 ~ 1,
                               statement == 23 ~ -1,
                               statement == 24 ~ -1,
                               statement == 25 ~ 1, #7
                               statement == 26 ~ 1,
                               statement == 27 ~ -1,
                               statement == 28 ~ -1,
                               statement == 29 ~ 1, #8
                               statement == 30 ~ 1,
                               statement == 31 ~ -1,
                               statement == 32 ~ -1,
                               statement == 33 ~ 1, #9
                               statement == 34 ~ 1,
                               statement == 35 ~ -1,
                               statement == 36 ~ -1))

# 5. scale the remainder so that they range from -2 to 2

data_raw <- mutate(data_raw,
                   pre_issue = 
                     case_when(pre_issue == 1 ~ -2,
                               pre_issue == 2 ~ -1,
                               pre_issue == 3 ~ 0,
                               pre_issue == 4 ~ 1,
                               pre_issue == 5 ~ 2,))

data_raw <- mutate(data_raw,
                   pre_cand = 
                     case_when(pre_cand == 1 ~ -2,
                               pre_cand == 2 ~ -1,
                               pre_cand == 3 ~ 0,
                               pre_cand == 4 ~ 1,
                               pre_cand == 5 ~ 2,))

data_raw <- mutate(data_raw,
                   post_issue = 
                     case_when(post_issue == 1 ~ -2,
                               post_issue == 2 ~ -1,
                               post_issue == 3 ~ 0,
                               post_issue == 4 ~ 1,
                               post_issue == 5 ~ 2,))

data_raw <- mutate(data_raw,
                   post_cand = 
                     case_when(post_cand == 1 ~ -2,
                               post_cand == 2 ~ -1,
                               post_cand == 3 ~ 0,
                               post_cand == 4 ~ 1,
                               post_cand == 5 ~ 2,))

write.csv(data_raw, file ="cleaned_experiment_300125.csv", row.names = FALSE) # adjust for own WD

### continue with do file in stata ###